﻿class SoundChannel {
	var addListener:Function;
	var removeListener:Function;
	var broadcastMessage:Function;
	
	var _playing:Boolean;
	var _volume:Number;
	
	function SoundChannel() {
		AsBroadcaster.initialize(this);
		_playing = false;
		_volume = 100;
	}
	
	function add(sound:ManagedSound):Void {
		if (sound._channel) {
			sound._channel.remove(sound);
		}
		sound.stop();
		sound._channel = this;
		sound.setVolume(_volume);
		this.addListener(sound);
		if (_playing) {
			sound.start();
		}
	}
	
	function addFromLibrary(id:String):Void {
		this.add(SoundManager.soundFromLibrary(id));
	}
	
	function addFromFile(file:String):Void {
		this.add(SoundManager.soundFromFile(file));
	}

	function remove(sound:ManagedSound):Void {
		this.removeListener(sound);
	}
	
	function start():Void {
		_playing = true;
		broadcastMessage('start');
	}
	
	function stop():Void {
		_playing = false;
		broadcastMessage('stop');
	}
	
	function pause():Void {
		_playing = false;
		broadcastMessage('pause');
	}
	
	function resume():Void {
		_playing = true;
		broadcastMessage('resume');
	}
	
	function getVolume():Number {
		return _volume;
	}
	
	function setVolume(volume:Number):Void {
		_volume = volume;
		broadcastMessage('setVolume', volume);
	}
}
